package com.education.wisdom.controller;


import com.github.pagehelper.PageHelper;
import com.education.common.utils.ApiReturnObject;
import com.education.wisdom.controller.management.CatalogController;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.pojo.Resources;
import com.education.wisdom.service.ICatalogService;
import com.education.wisdom.service.IResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author xc
 */
@RestController
@RequestMapping("/ZfyCatalog")
public class ZfyCatalogController {

    //课程
    private final ICatalogService catalogService;
    //资源
    private final IResourcesService resourcesService;

    private final RedisTemplate redisTemplate;

    @Autowired
    public ZfyCatalogController(ICatalogService catalogService,IResourcesService resourcesService,RedisTemplate redisTemplate) {
        this.catalogService = catalogService;
        this.resourcesService = resourcesService;
        this.redisTemplate = redisTemplate;
    }

    /**
     *  递归嵌套下级目录 用一个集合进行接受 3级
     */
    private List<Catalog> catalogAll(List<Catalog> firstList){
        for (Catalog catalog : firstList) {
            List<Catalog> catalogs = catalogService.selectByParentId2(catalog.getCatalogId());
            if(catalogs.size() > 0){
                return catalogAll(catalogs);
            }else {
                catalog.setCatalogs(catalogs);
            }
        }
        return firstList;
    }


    /**
     *   教学大纲目录查询
     */
    @GetMapping("listMenu")
    public ApiReturnObject listMenu(Catalog catalog,Integer pageNum,Integer pageSize){
        catalog.setParentId(0);
        List<Catalog> catalogs = catalogService.selectByParentId2(0);
        //设置下级目录列表
        catalogAll(catalogs);
        return new ApiReturnObject("200","查询成功",catalogs);
    }

    /**
     * 教学大纲添加 目录等级
     */
    @PostMapping("addMenu")
    public ApiReturnObject addMenu(@RequestBody Catalog catalog){
        redisTemplate.delete("catalogList");
        //删除redis中数据
       catalog.setCreateTime(new Date());
       catalogService.insertCatalog(catalog);
       return new ApiReturnObject("200","添加成功","");
    }

    /**
     * 教学大纲详情
     */
    @GetMapping("/selectOneMenu")
    public ApiReturnObject selectOneMenu(Integer id){
        if(id == null){
            return new ApiReturnObject("201","缺少参数id","");
        }
        Catalog catalog = catalogService.selectCatalogByCatalogId(id);
        return new ApiReturnObject("200","查询成功",catalog);
    }


    /**
     * 教学大纲编辑
     */
    @PostMapping("updateMenu")
    public ApiReturnObject updateMenu(@RequestBody Catalog catalog ){
       if(catalog.getCatalogId() != null ){
           redisTemplate.delete("catalogList");
           catalogService.updateCataLog(catalog);
           return new ApiReturnObject("200","修改成功","");
       }
       return new ApiReturnObject("201","缺少参数id","");
    }


    /**
     * 教学大纲删除
     */
    @PostMapping("delMenu")
    public ApiReturnObject delMenu(@RequestBody Catalog catalog){
       if(catalog.getCatalogId() != null){
           redisTemplate.delete("catalogList");
           catalogService.delectCataLogById(catalog.getCatalogId().toString());
           return new ApiReturnObject("200","删除成功","");
       }
       return new ApiReturnObject("201","缺少参数","");
    }


    /** 递归 筛选第三级目录 还是 查最后一级  */
    public void lastOne(){


    }

    @GetMapping("/list2")
    public void list2() {
        //当redis不为空 直接从redis拿数据
        List redisCatalogs = (List) redisTemplate.opsForValue().get("catalogList");
        if (redisCatalogs == null) {
            List<Catalog> list = catalogService.selectByParentId(0);
            catalogAll(list);
            redisTemplate.opsForValue().set("catalogList", list);
        }
    }

    /**
     *  教学云课堂列表编写 第三级   分页 catalog_sort 3
     */
    @GetMapping("/catalogList")
    public ApiReturnObject catalogList(Integer pageNum,Integer pageSize,Catalog catalog){
        //匿名内部类创建新线程    判断redis中书否存在数据    没有的话 从数据库查询并添加值缓存中
        new Thread(this::list2).start();

        catalog.setCatalogSort(3);
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<Catalog> catalogs = catalogService.selectCatalogList2(catalog);
        return new ApiReturnObject("200","查询成功",catalogs);
    }



    /** 资源列表 */
    @GetMapping("list")
    public ApiReturnObject list(Resources resources){
        //条件  catalogId 类型
        List<Resources> resources1 = resourcesService.selectResourcesList(resources);
        return new ApiReturnObject("200","查询成功",resources1);
    }


    /** 添加资源 */
    @PostMapping("add")
    public ApiReturnObject add(@RequestBody Resources resources){
       resources.setCreateTime(new Date());
       resourcesService.insertResources(resources);
       return new ApiReturnObject("200","添加成功","");
    }


    /** 资源详情 */
    @GetMapping("/selectOneById")
    public ApiReturnObject selectOneById(Integer id){
        if(id != null ){
            Resources resources = resourcesService.selectResourcesById(id.toString());
            return new ApiReturnObject("200","查询成功",resources);
        }
        return new ApiReturnObject("201","缺少参数id","");
    }


    /** 修改资源 */
    @PostMapping("update")
        public ApiReturnObject update(@RequestBody Resources resources ){
           if(resources.getId() != null ){
               resourcesService.updateResources(resources);
               return new ApiReturnObject("200","修改成功","");
           }
           return new ApiReturnObject("201","缺少参数id","");
       }


    /** 删除资源 */
    @PostMapping("del")
    public ApiReturnObject del(@RequestBody Resources resources){
       if(resources.getId() != null){
           resourcesService.deleteResourcesByIds(resources.getId().toString());
           return new ApiReturnObject("200","删除成功","");
       }
       return new ApiReturnObject("201","缺少参数","");
    }


}
